package com.qiandw.enterprise.credit.service

import java.util

import com.qiandw.enterprise.credit.listToJsonStrWithTotalCount
import com.qiandw.enterprise.credit.model.{ModuleTreeGrid, Tree}
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

import scala.collection.JavaConversions._

/**
  * Created by Draven on 2018/7/10.
  * Email:hjx@qiandw.com
  */
@Service
class EasyUISrvImp extends EasyUISrv {

  @Autowired
  var moduleSrv: ModuleSrv = _

  override def getModuleTree: util.List[Tree] = {
    val modules = moduleSrv.list1LevelModules()
    var list = new util.ArrayList[Tree]()
    modules.foreach(m => {
      val tree = new Tree
      tree.id = m.id
      tree.text = m.name
      if (!m.child.isEmpty) {
        m.child.foreach(child => {
          val c = new Tree
          c.id = child.id
          c.text = child.name
          tree.children.add(c)
        })
      }
      list.add(tree)
    })
    list
  }

  override def getModuleTreeGrid: String = {
    val modules = moduleSrv.listAllModules()
    val moduleTreeGrids = new util.ArrayList[ModuleTreeGrid]()
    modules.foreach(m => {
      val moduleTreeGrid = new ModuleTreeGrid
      moduleTreeGrid.parentId = m.parentId
      moduleTreeGrid.id = m.id
      moduleTreeGrid.name = m.name
      moduleTreeGrid.url = m.url
      moduleTreeGrid.remark = m.remark
      moduleTreeGrid.level = m.level
      moduleTreeGrid.seq = m.seq
      moduleTreeGrids.add(moduleTreeGrid)
    })
    val jsonStr = listToJsonStrWithTotalCount(moduleTreeGrids)
    jsonStr
  }
}
